const express = require('express')
const {
  queryProvinceIdByProvinceName,
  queryCityIdByCityName,
  queryCountyIdByCountyName,
  queryWayIdByWayName,
  insertAddress,
  updateAddress,
  queryAddressLength,
  queryAddressList,
  queryProvinceList,
  queryCityList,
  queryCountyList,
  queryWayList,
  queryAddressListByWayName,
  updateAddressForDelByAddressId
} = require('../mysql/address')
const Result = require('../result/result')
const { NO_PERMISSION } = require('../result/reponseState')

// address
const router = express.Router()
router.get('', (req, res) => {
  const { userId, currentPage, pageSize } = req.query
  queryAddressList(userId, currentPage, pageSize).then((response) => {
    queryAddressLength().then((response2) => {
      res.send(Result.ok2(response, response2[0].length))
    })
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/province', (req, res) => {
  queryProvinceList().then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/city', (req, res) => {
  const provinceId = req.query.provinceId
  queryCityList(provinceId).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/county', (req, res) => {
  const cityId = req.query.cityId
  queryCountyList(cityId).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/way', (req, res) => {
  const countyId = req.query.countyId
  queryWayList(countyId).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.get('/search', (req, res) => {
  const wayName = req.query.wayName
  queryAddressListByWayName(wayName).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.put('/update', (req, res) => {
  const address = req.body.address
  updateAddress(address).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.post('/insert', async (req, res) => {
  const { address, user_id } = req.body
  const { province_name, city_name, county_name, way_name } = address
  const province_id = await queryProvinceIdByProvinceName(province_name)
  const city_id = await queryCityIdByCityName(city_name)
  const county_id = await queryCountyIdByCountyName(county_name)
  const way_id = await queryWayIdByWayName(way_name)
  insertAddress({ ...address, ...province_id[0], ...city_id[0], ...county_id[0], ...way_id[0] }, user_id).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

router.delete('/delete', (req, res) => {
  const address_id = req.query.address_id
  updateAddressForDelByAddressId(address_id).then((response) => {
    res.send(Result.ok(response))
  }, (err) => {
    res.send(Result.error2(NO_PERMISSION, err))
  })
})

module.exports = router